using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Image2d
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        //
        Texture2D ship;
        //
        KeyboardState teclado;
        //
        Vector2 position;
        //
        Vector2 velocity;

        /*
        Vector2 direction;
        float Vel;
        int VelMax;
        */

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            position = Vector2.Zero;
            velocity = new Vector2(10f, 10f);
           
            /*
            Vel = 0f;
            VelMax = 15;
            */
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            ship = Content.Load<Texture2D>("Texture/Ship"); 

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {

        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            teclado = Keyboard.GetState();

            if (teclado.IsKeyDown(Keys.A) || teclado.IsKeyDown(Keys.Left))
            {
                position.X -= velocity.X;
            }
            else if (teclado.IsKeyDown(Keys.D) || teclado.IsKeyDown(Keys.Right))
            {
                position.X += velocity.X;
            }
            if (teclado.IsKeyDown(Keys.S) || teclado.IsKeyDown(Keys.Down))
            {
                position.Y += velocity.Y;
            }
            else if (teclado.IsKeyDown(Keys.W) || teclado.IsKeyDown(Keys.Up))
            {
                position.Y -= velocity.Y;
            }

            //teleport ship from one side to other
            if (position.X < -Window.ClientBounds.Width / 2 - ship.Width)
            {
                position.X = Window.ClientBounds.Width / 2;

            }

            /*
            //rotation update
            if (Input.GetKey(Keys.A) || Input.GetKey(Keys.Left))
            {
                rotation -= 0.1f;
            } 
            else if (Input.GetKey(Keys.D) || Input.GetKey(Keys.Right))
            {
                rotation += 0.1f;
            }
            if (Input.GetKey(Keys.W) || Input.GetKey(Keys.Up))
            {
                Vel += 0.1f;
                if (Vel > MAX_VEL) Vel = MAX_VEL;
            }
            else if (Input.GetKey(Keys.S) || Input.GetKey(Keys.Down))
            {
                Vel -= 0.1f;
                if (Vel < -MAX_VEL) Vel = -MAX_VEL;
            }
            else
            {
                if (Vel > 0f) Vel -= 0.1f;
                else if (Vel < 0f) Vel += 0.1f;
                else Vel = 0;

            } 
            direction = new Vector2((float)Math.Sin(Rotation), -(float)Math.Cos(Rotation));
            Position += Vel * direction;
            */
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.AliceBlue);

            spriteBatch.Begin();


            spriteBatch.Draw(ship,
                new Vector2(Window.ClientBounds.Width / 2 + position.X , Window.ClientBounds.Height / 2 +position.Y),
                Color.White);

           
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
